package org.oreilly.javaGoodParts.examples;

public class BatterStats {

    private int atBats;
    private int hits;
    private int bases;
    private int baseOnBalls;
    private int sacrifices;

    public BatterStats(Iterable<Event> events) {
		for (Event event : events) {
			if (event instanceof BattingEvent) {
				atBat(((BattingEvent) event).getResult());
			}
		}
    }

    private void atBat(BattingEvent.Result what) {
        switch (what) {
            case STRIKE_OUT:
                break;
            case FIELD_OUT:
                break;
            case BASE_1:
            	hits++;
            	bases++;
                break;
            case BASE_2:
            	hits++;
            	bases += 2;
                break;
            case BASE_3:
            	hits++;
            	bases += 3;
            case BASE_4:
            	hits++;
            	bases += 4;
            case SACRIFICE:
            	sacrifices++;
                return;
            case WALK:
            	baseOnBalls++;
                return;
        }
        atBats++;
    }

    public int getAtBats() {
        return atBats;
    }

    public float getAverage() {
        return (float) hits / atBats;
    }

    public float getOBP() {
        return ((float) hits + baseOnBalls) / (atBats + sacrifices + baseOnBalls);
    }

    public float getSlugging() {
        return (float) bases / atBats;
    }

    public int getTotalBases() {
        return bases;
    }
}